<?php
namespace Com\Pay;
class zhongyiPay{

    private $config = array();


    public function __construct($send_data){
        $this->config = $send_data;
    }

    /*生成支付连接*/
    public function getUrl($order_info){
        $price = $order_info['paymoney'];
        $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/zhongyiPayNotify';
        $Sumprice = $price * 100;
        $order_sn = $order_info['ordernumber'];
        $post_data = array(
            'type' =>   $order_info['type'],
            'ins_id'    =>     $this->config['mch_id'],
            "total_amt"  =>  $Sumprice,
            'ins_order_id' => $order_sn,
            'notify_url'    => $notifyUrl,
        );
        $ContentSign = $this->setParam($post_data,$this->config['mch_key'],false); // 计算出Sign
        $post_data['sign'] = $ContentSign; // Sign签名
//        var_dump($post_data);exit;
        $tar_url = 'http://123.182.255.83:8090/NMobPay/QuickPay.json';
        $res = $this->https_request($tar_url,$post_data);
        var_dump($res);die;
    }

    private function setParam($param,$key,$IS_Notify){
        $md5string = 'type=' . $param['type'] . '&ins_id=' . $param['ins_id'] . '&total_amt='.$param['total_amt'] . '&ins_order_id=' . $param['ins_order_id'] . '&notify_url=' . $param['notify_url'] .$key;
        $sign = md5($md5string);
        return $sign;
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = $_REQUEST;
        $MySign = $this->setParam($data,$this->config['mch_key'],true);
        if($data['sign'] == $MySign){
            if($data['pay_result'] == '0' && $data['status'] == '0'){
                return true;
            }else{
                // 失败
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }


    private function https_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        //参数不为空则为post请求

        if (!empty($data)){

            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}
